home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_6_3.arc / LISTING2 < prev    next >
Text File  |  1988-04-05  |  2KB  |  83 lines

  1. Listing 2 from "A CASE of the Jumps" by Tom Swan.  Copyright 1988
  2. by Tom Swan.  No commercial use of this code without express
  3. permission of the author.
  4.  
  5. ;-----  This program demonstrates how to write jump (case) tables
  6. ;       in 8086-family assembly language.  For MASM 5.0.
  7.  
  8.         TITLE   CASE.ASM
  9.         PAGE    60,132
  10.         DOSSEG
  11.         .MODEL  small
  12.         .STACK  256
  13.  
  14.         .DATA
  15.  
  16. ;-----  Jump (case) table of addresses
  17.  
  18. table           dw      case0, case1, case2, case3
  19.  
  20. ;-----  Various strings
  21.  
  22. prompt          db      'Enter 0, 1, 2, or 3: ', '$'
  23. msg0            db      13, 10, 'Case statement #0', '$'
  24. msg1            db      13, 10, 'Case statement #1', '$'
  25. msg2            db      13, 10, 'Case statement #2', '$'
  26. msg3            db      13, 10, 'Case statement #3', '$'
  27. errmsg          db      13, 10, 'Data entry error', '$'
  28.  
  29.         .CODE
  30. start:
  31.         mov     ax,@data                ; initialize DS
  32.         mov     ds,ax
  33.  
  34.         mov     dx,offset prompt        ; display prompt
  35.         mov     ah,09h
  36.         int     21h
  37.  
  38.         mov     ah,01                   ; get character
  39.         int     21h
  40.  
  41. ;-----  Test range of char in al.  Prepare bx equal to the case
  42. ;       statement number times 2, indexing the 2-byte addresses in
  43. ;       the jump table.
  44.  
  45.         mov     dx,offset errmsg        ; prepare for possible error
  46.         cmp     al,'0'
  47.         jb      endcase                 ; error if al < '0'
  48.         cmp     al,'3'
  49.         ja      endcase                 ; error if al > '3'
  50.  
  51.         mov     bl,al                   ; move digit char to bl
  52.         and     bx,03h                  ; convert to value 0,1,2,3
  53.         shl     bx,1                    ; multiply bx by 2
  54.         jmp     table[bx]               ; jump to case 0,1,2 or 3
  55.  
  56. ;-----  Each case is a separate routine, ending with a jump to the
  57. ;       end-of-case label.
  58.  
  59. case0:
  60.         mov     dx,offset msg0
  61.         jmp     endcase
  62. case1:
  63.         mov     dx,offset msg1
  64.         jmp     endcase
  65. case2:
  66.         mov     dx,offset msg2
  67.         jmp     endcase
  68. case3:
  69.         mov     dx,offset msg3
  70.  
  71. ;----- Assume dx addresses ASCII$ message to display
  72.  
  73. endcase:
  74.         mov     ah,09h                  ; display message at dx
  75.         int     21h
  76.         mov     ax,04C00h               ; end with exit code = 0
  77.         int     21h
  78.  
  79.         END     start                   ; end of text, entry point
  80.  
  81.  
  82. Listing 2
  83.